
package com.kugou.game.sdk.core;

import java.util.ArrayList;

import android.os.Handler;
import android.text.TextUtils;

import com.kugou.framework.base.LogUtil;
import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.interfaces.OnRequestVipInfoListener;
import com.kugou.game.sdk.protocol.VipTask;
import com.kugou.game.sdk.protocol.VipTask.VipResponse;
import com.kugou.game.sdk.utils.ThreadTaskManager;

/**
 * 描述:vip的数据管理
 *
 * @author wcj
 * @since 2016年1月16日 下午2:39:21
 */
public class VipManager {

    public static class GSInfo {
        String name;

        String qq;

        String imgUrl;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getQq() {
            return qq;
        }

        public void setQq(String qq) {
            this.qq = qq;
        }

        public String getImgUrl() {
            return imgUrl;
        }

        public void setImgUrl(String imgUrl) {
            this.imgUrl = imgUrl;
        }

    }

    public static class VipInfo {
        boolean isVip = false;

        // vip专题url
        String vipTopicUrl;

        // vip专题标题
        String vipTopicTitle;

        // vip广告图片
        String vipAdImgUrl;

        // vip专属gs信息
        ArrayList<GSInfo> gsInfos = new ArrayList<GSInfo>();

        // gs群的图片竖屏
        String gsImageUrlPortait;

        // gs群的图片横屏
        String gsImageUrlLandscape;

        // gs群的qq
        String gsQQ;

        public boolean isVip() {
            return isVip;
        }

        public void setVip(boolean isVip) {
            this.isVip = isVip;
        }

        public String getVipTopicUrl() {
            return vipTopicUrl;
        }

        public void setVipTopicUrl(String vipTopicUrl) {
            this.vipTopicUrl = vipTopicUrl;
        }

        public String getVipTopicTitle() {
            return vipTopicTitle;
        }

        public void setVipTopicTitle(String vipTopicTitle) {
            this.vipTopicTitle = vipTopicTitle;
        }

        public String getVipAdImgUrl() {
            return vipAdImgUrl;
        }

        public void setVipAdImgUrl(String vipAdImgUrl) {
            this.vipAdImgUrl = vipAdImgUrl;
        }

        public ArrayList<GSInfo> getGsInfos() {
            return gsInfos;
        }

        public void setGsInfos(ArrayList<GSInfo> gsInfos) {
            this.gsInfos = gsInfos;
        }

        public String getGsImageUrlPortait() {
            return gsImageUrlPortait;
        }

        public void setGsImageUrlPortait(String gsImageUrlPortait) {
            this.gsImageUrlPortait = gsImageUrlPortait;
        }

        public String getGsImageUrlLandscape() {
            return gsImageUrlLandscape;
        }

        public void setGsImageUrlLandscape(String gsImageUrlLandscape) {
            this.gsImageUrlLandscape = gsImageUrlLandscape;
        }

        public String getGsQQ() {
            return gsQQ;
        }

        public void setGsQQ(String gsQQ) {
            this.gsQQ = gsQQ;
        }

    }

    private static final int MAX_REQUEST_TIME = 5;

    private static VipManager sVipManager;

    private VipInfo mVipInfo;

    private static int sReRequestTime = 0;

    private VipManager() {
        mVipInfo = new VipInfo();
    }

    public static VipManager getInstance() {
        synchronized (VipManager.class) {
            if (sVipManager == null) {
                sVipManager = new VipManager();
            }
        }

        return sVipManager;
    }

    public void requestVipInfos(final boolean onlyRequestVipBaseInfo,
            final OnRequestVipInfoListener listener) {
        ThreadTaskManager.excuteTask(new Runnable() {

            @Override
            public void run() {
                VipTask task = new VipTask();
                User user = UserManager.getInstance().getCurrentUserInternal();

                VipResponse response = task.request(onlyRequestVipBaseInfo, user == null ? ""
                        : user.getAccountName());
                if (response != null) {
                    if (response.isUseful() && response.isOk()) {
                        sReRequestTime = 0;
                        setVipInfo(response.getVipInfo());
                        if (listener != null) {
                            listener.onSuccess();
                        }
                    } else {
                        if (!TextUtils.isEmpty(response.getPrompt())) {
                            LogUtil.d(response.getPrompt());
                        }
                        requestNextTime();
                    }
                } else {
                    LogUtil.d("网络请求失败");
                    requestNextTime();
                }
            }
        });

    }

    /**
     * 请求失败重新请求，最大次数为MAX_REQUEST_TIME
     */
    private void requestNextTime() {
        if (sReRequestTime > MAX_REQUEST_TIME) {
            return;
        }

        sReRequestTime++;
        new Handler(SDKControler.getContext().getMainLooper()).postDelayed(new Runnable() {

            @Override
            public void run() {
                requestVipInfos(false, null);

            }
        }, 1500);
    }

    public VipInfo getVipInfo() {
        return mVipInfo;
    }

    public void setVipInfo(VipInfo vipInfo) {
        if (vipInfo != null) {
            this.mVipInfo = vipInfo;
        }
    }

    public boolean isVip() {
        if (getVipInfo() != null) {
            return getVipInfo().isVip;
        } else {
            return false;
        }
    }

    public String getGsImage() {
        if (getVipInfo() != null) {
            if (SDKControler.getActivityOrientation() == ActivityOrientation.PORTRAIT) {
                return getVipInfo().getGsImageUrlPortait();
            } else {
                return getVipInfo().getGsImageUrlLandscape();
            }
        } else {
            return "";
        }
    }
}
